package icu.sehnen.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import icu.sehnen.mapper.UserMapper;
import icu.sehnen.model.UserModel;
import icu.sehnen.service.UserService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public  class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Integer login(String username,String password) {
        return  userMapper.findUser(username,password);
    }

    @Override
    public Integer register(UserModel user) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strDate = format.format(date);
        user.setCreateTime(strDate);
        user.setUpdateTime(strDate);
        return userMapper.addUser(user);

    }

    @Override
    public PageInfo<UserModel> getUsers(Integer pageNum, Integer pageSize,String query) {
         PageHelper.startPage(pageNum,pageSize);
         List<UserModel> userModelList=userMapper.findAll(query);
         PageInfo<UserModel> pageInfo=new PageInfo<>(userModelList);
         System.out.println(pageInfo);
         return  pageInfo;
    }

    @Override
    public Integer swichStats(Long id,Integer status ) {
       return  userMapper.swichStats(id,status);
    }

    @Override
    public Integer addUser(UserModel userModel) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strDate = format.format(date);
        userModel.setCreateTime(strDate);
        userModel.setUpdateTime(strDate);
        return  userMapper.addUser(userModel);
    }

    @Override
    public UserModel findUserbyId(Long id) {
        return userMapper.findUserById(id);
    }

    @Override
    public Integer deleteUserbyId(Long id) {
        return  userMapper.deleteUserById(id);
    }


}
